using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Test;

namespace PdfLib.Test
{
    public partial class MainTester : Form
    {
        private Dictionary<string, List<Type>> _testerControlTypes;

        public MainTester()
        {
            InitializeComponent();
        }

        private void MainTester_Load(object sender, EventArgs e)
        {
            this.PopulateAvailableTestCases();
            this.DisplayAvailableTestCases();
        }

        private void PopulateAvailableTestCases()
        {
            List<Type> allTestControlTypes = ManualTestUtil.GetAllManualTestControlTypes();
            this._testerControlTypes = new Dictionary<string, List<Type>>();
            foreach (Type type in allTestControlTypes)
            {
                ManualTestCase manualTest =
                    (ManualTestCase) type.GetCustomAttributes(typeof (ManualTestCase), false)[0];
                string categoryName = manualTest.CategoryName;
                if (this._testerControlTypes.ContainsKey(categoryName))
                {
                    List<Type> testCtrTypes = this._testerControlTypes[categoryName];
                    testCtrTypes.Add(type);
                    this._testerControlTypes[categoryName] = testCtrTypes;
                }
                else
                {
                    List<Type> testCtrTypes = new List<Type>();
                    testCtrTypes.Add(type);
                    this._testerControlTypes.Add(categoryName, testCtrTypes);
                }
            }
        }

        private void DisplayAvailableTestCases()
        {
            this.tree_TestCases.Nodes.Clear();
            if (this._testerControlTypes != null && this._testerControlTypes.Count > 0)
            {
                foreach (string categoryName in this._testerControlTypes.Keys)
                {
                    TreeNode categoryNode = new TreeNode(categoryName);
                    categoryNode.ImageIndex = 0;
                    categoryNode.SelectedImageIndex = 0;
                    this.tree_TestCases.Nodes.Add(categoryNode);

                    List<Type> testCtrTypes = this._testerControlTypes[categoryName];
                    foreach (Type testCtrType in testCtrTypes)
                    {
                        ManualTestCase manualTest =
                            (ManualTestCase) testCtrType.GetCustomAttributes(typeof (ManualTestCase), false)[0];
                        TreeNode testNode = new TreeNode(manualTest.TestCaseName);
                        testNode.ImageIndex = 1;
                        testNode.SelectedImageIndex = 1;
                        testNode.Tag = testCtrType;
                        categoryNode.Nodes.Add(testNode);
                    }
                }
            }
        }

        private void tree_TestCases_MouseUp(object sender, MouseEventArgs e)
        {
            this.tree_TestCases.SelectedNode = this.tree_TestCases.GetNodeAt(e.X, e.Y);
            if (this.tree_TestCases.SelectedNode != null && this.tree_TestCases.SelectedNode.ImageIndex == 1)
            {
                Type testCtrType = (Type)this.tree_TestCases.SelectedNode.Tag;
                UserControl testUc = (UserControl)Activator.CreateInstance(testCtrType);
                this.p_Workspace.Controls.Clear();
                if (testUc != null)
                {
                    this.p_Workspace.Controls.Add(testUc);
                    testUc.Dock = DockStyle.Fill;
                }
            }
        }
    }
}